﻿

using F29ServerProcess.Interfaces;
using IotContract.Dtos.Servers.Process;
using IotContract.Dtos.Servers.Process.F29;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _29ServerProcess.Services;
/// <summary>
/// 设备验证服务
/// </summary>
public class EquipValidateService : IEquipValidateService
{
    /// <summary>
    /// 验证物流线状态
    /// </summary>
    /// <param name="cv"></param>
    /// <param name="hasTray">是否有托盘</param>
    /// <returns></returns>
    public bool ValidateCV(F29CvUnitDto cv, bool hasTray=true)
    {
        if (cv == null)
        {
            return false;
        }
        // 不是实时数据
        if (cv.Connection==false)
        {
            return false;
        }

        // 存在报警
        if (cv.Error != 0)
        {

            return false;
        }
        // 处于手动模式或者断电

        if (hasTray)
        {
            // 不存在托盘，验证失败
            if (!cv.IsExistTray1 || cv.TaskNumber==0)
            {
                return false;
            }
        }
        else
        {
            if(cv.IsExistTray1 || cv.TaskNumber > 0)
            {
                return false;
            }
        }
       
        return true;

    }
    /// <summary>
    /// 验证堆垛机
    /// </summary>
    /// <param name="sc"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool ValidateStacker(F29ScUnitDto sc)
    {
        if (sc == null)
        {
            return false;
        }
        // 不是实时数据
        if (sc.ReadTime==null || !sc.Connection || DateTime.Now.Subtract(sc.ReadTime.Value).TotalSeconds>15)
        {           
            return false;
        }
        if (sc.Action == 1)
        {
            return false;
        }

        // 存在报警
        if (sc.Error != 0)
        {

            return false;
        }
        // 处于手动模式或者断电
        if (sc.Mode == 2 || sc.Power==0)
        {
            return false;
        }
        return true;
    }
}
